/**
 * @description user的路由控制器
 * @author 盘古他爹
 */
//  require('module-alias/register');
import UserService from "../service/user/user.service"
import ResponseModel from "../config/response.config"
//错误提示信息
import { userDoNotExist, passwordError, registerError, updateUserInfoError, passwordSameError, oldPasswordError } from "../config/errorInfo"
//引入bcrypt加密
import { hashStr, compareHash } from "../utils/bcrypt"
//引入jwt
import { JwtSign } from "../utils/jwt"
//获取该角色前端路由
import { roleReturnMenu } from "../config/frontRouterConfig"
import { C, N } from "../types"



class UserController   {

      async userRegister(ctx: C) {
        //注册
        
        console.log("userName", ctx.request.body);
        const userInfo = ctx.request.body
        
       try {
        const res = await UserService.getUserInfo(userInfo)
        if (!res) {
            //用户名不存在可以注册用户
            const userInfo = ctx.request.body
            userInfo.password = hashStr(userInfo.password)
            const res = await UserService.registerUser(userInfo)
            delete res.password
            ctx.body = ResponseModel.success(res)
            return
        }
        //用户名已存在
        ctx.body = ResponseModel.error(userDoNotExist)
       } catch (err) {
           console.error(err)
           ctx.body = ResponseModel.error(registerError)
       }
       
    //    ctx.body = "注册" 
        }

    async userLogin(ctx: C) {
        //登录查询用户名数据
        const { userName, password} = ctx.request.body
        console.log("到这了", userName, password);
        
        const userInfo = await UserService.getUserInfo({userName})
        
        // console.log("登录的res",userInfo, compareHash(password, userInfo.password));
        
            //比较密码
            if (userInfo && compareHash(password, userInfo.password)) {
                // delete userInfo.password  token要不要保存密码
                console.log("userInfo", userInfo);
                const token = JwtSign(userInfo)
                
                delete userInfo.status
                
                ctx.body = ResponseModel.success({
                    token,
                    menus: roleReturnMenu(userInfo.role),
                    ...userInfo
                },`登陆成功,${userName}欢迎回来`)
                return
            }
            
            ctx.body = ResponseModel.error(passwordError)

        }

    async updateUserInfo(ctx: any) {
        const newUserInfo = ctx.request.body
        //新旧密码相同，返回错误
        if (newUserInfo.oldPassword === newUserInfo.newPassword) {
            ctx.body = ResponseModel.error(passwordSameError)
            return
        }
        //查询旧密码，保证旧密码一定存在且正确
        const checkPasswordRes = 
        compareHash(newUserInfo.oldPassword, ctx.userInfo.password)
        console.log("checkPasswordRes", checkPasswordRes);
        
        if (!checkPasswordRes) {
            ctx.body = ResponseModel.error(oldPasswordError)
            return
        }
        //新旧密码不同，
        if (newUserInfo.oldPassword) {
            newUserInfo.newPassword = hashStr(newUserInfo.newPassword)
        }
        const userInfo = {
            password: "",
            nickName: ""
        }
         userInfo.password = newUserInfo.newPassword
         userInfo.nickName = newUserInfo.nickName

        const res = await UserService.updateUserInfo(userInfo, ctx.userInfo.id)
        if (res) {
            ctx.body = ResponseModel.success({},"修改信息成功！")
            return
        }
        ctx.body = ResponseModel.error(updateUserInfoError)
        }

}

export default new UserController()

